//
//  ZMBLineProgressView.swift
//  HengYouCai
//
//  Created by wll on 2018/5/10.
//  Copyright © 2018年 lwj. All rights reserved.
//  直线进度视图--代码

private let selfH: CGFloat = 6
private let bgColor = ColorFromRGBHex_WL(0xd9d9d9)
private let progressColor = Normal_ThemeColor
///渐变色梯度1 浅红
private let shadeColor1: CGColor = ColorFromRGBHex_WL(0x5b9cf8).cgColor
///渐变色梯度2 深红
private let shadeColor2: CGColor = ColorFromRGBHex_WL(0x656dfe).cgColor

import UIKit

class ZMBLineProgressView: UIView {
    
    ///记录值
    private var progress: CGFloat = 0
    private var status: JCWProductStatus?
    
    ///0-1 CGFloat 标的状态
    func wl_setProgress(_ progress: CGFloat, _ status: JCWProductStatus?) {
        self.progress = progress
        self.status = status
        self.setNeedsLayout()
    }


    override init(frame: CGRect) {
        super.init(frame: frame)
        self.createCV()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func createCV() {
        self.layer.cornerRadius = selfH/2
        self.clipsToBounds = true
        self.addSubview(self.bgView)
        self.addSubview(self.progressView)
        self.progressView.layer.addSublayer(self.gradientLayer)
        //有一个终点视图 干掉 如有需要打开
//        self.addSubview(self.endImageV)
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        self.backgroundColor = UIColor.yellow
        self.height_sd = selfH
        self.bgView.frame = self.bounds
        if self.status == nil {
            return;
        }
        
        if self.status == .prepareSale {//预售中 20
            self.isHidden = true
        }else if status == .saling{//募集中 30
            self.progressView.frame = CGRect(x: 0, y: 0, width: self.width_sd * self.progress, height: selfH)
//            self.endImageV.centerX_sd = self.progressView.right_sd
//            self.endImageV.centerY_sd = self.bgView.centerY_sd
            self.isHidden = false
        }else{//已售罄
            self.progressView.frame = self.bounds
//            self.endImageV.centerX_sd = self.bgView.right_sd
//            self.endImageV.centerY_sd = self.bgView.centerY_sd
            self.isHidden = false
        }
        self.gradientLayer.frame = self.progressView.bounds

    }
    
    ///背景view
    let bgView: UIView = {
        let temp = UIView()
        temp.backgroundColor = bgColor
//        temp.layer.cornerRadius = selfH/2
        return temp
    }()
    
    ///进度view
    let progressView: UIView = {
        let temp = UIView()
        temp.backgroundColor = progressColor
//        temp.layer.cornerRadius = selfH/2
        
        return temp
    }()
    
    ///渐变图层 左右
    lazy var gradientLayer: CAGradientLayer = {
        let temp = CAGradientLayer()
        temp.colors = [shadeColor1,shadeColor2]
//        temp.locations = [0,0.5]
        temp.startPoint = CGPoint(x: 0, y: 0)
        temp.endPoint = CGPoint(x: 1, y: 0)
        return temp
    }()
    
    ///终点标志
//    let endImageV: UIImageView = {
//        let temp = UIImageView(image: UIImage(named: "endimage"))
//        temp.bounds = CGRect(x: 0, y: 0, width: 7, height: 7)
//        return temp
//    }()
    
}



